﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Medianamik.Core.Configuration;
using System.Collections.Generic;
using System.IO;
using Medianamik.Core;
using Medianamik.UI.Web.TypeManagement.Controls;

namespace TypeManagement.Controls
{
    public partial class MultipleTemplatePicker : System.Web.UI.UserControl
    {
        #region Fields


        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            BuildSelectedTemplateList();

            if (!IsPostBack)
            {
                LoadTemplates();
                LoadWebsites();
            }
        }

        protected void AddTemplate(object sender, EventArgs e)
        {
            if (ddlTemplates.SelectedIndex > 0 && ddlWebsites.SelectedIndex > 0)
            {
                Guid websiteId = new Guid(ddlWebsites.SelectedValue);
                if (Templates.Any(t => t.WebSiteId.Equals(websiteId)))
                {
                    litMsg.Text = "<br />Un seul gabarit par WebSite.";
                }
                else
                {
                    TypeTemplate template = new TypeTemplate()
                    {
                        TemplateId = new Guid(ddlTemplates.SelectedValue),
                        WebSiteId = websiteId
                    };

                    phrTemplates.Controls.Add(CreateControl(template));
                    List<TypeTemplate> templates = Templates.ToList();
                    templates.Add(template);
                    Templates = templates.ToArray();
                }

                ddlWebsites.SelectedIndex = 0;
                ddlTemplates.SelectedIndex = 0;
            }
            else
            {
                litMsg.Text = "<br />Veuillez sélectionner un gabarit et un WebSite.";
            }
        }

        private void RemoveTemplate(object sender, EventArgs e)
        {
            TypeTemplate template = ((TemplatePicker)sender).Template;
            List<TypeTemplate> templates = Templates.ToList();
            templates.Remove(((TemplatePicker)sender).Template);
            Templates = templates.ToArray();
            phrTemplates.Controls.Remove(((Control)sender));
        }

        #endregion

        #region Properties

        public TypeTemplate[] Templates
        {
            get
            {
                if (_templates == null)
                {
                    if (ViewState["Templates"] == null)
                        _templates = new List<TypeTemplate>().ToArray();
                    else
                        _templates = (TypeTemplate[])ViewState["Templates"];
                }
                return _templates;
            }
            set
            {
                _templates = value;
                ViewState["Templates"] = _templates;
            }
        }
        TypeTemplate[] _templates = null;

        #endregion

        #region Methods

        private void BuildSelectedTemplateList()
        {
            foreach (TypeTemplate template in Templates)
            {
                phrTemplates.Controls.Add(CreateControl(template));
            }
        }

        private void LoadWebsites()
        {
            ddlWebsites.DataSource = ConfigManager.GetAll<WebSite>().OrderBy(t => t.DisplayName );
            ddlWebsites.DataTextField = "DisplayName";
            ddlWebsites.DataValueField = "ID";
            ddlWebsites.DataBind();
            ddlWebsites.Items.Insert(0, "");
        }

        private void LoadTemplates()
        {
            ddlTemplates.DataSource = ConfigManager.GetAll<PageTemplate>().OrderBy(t => t.DisplayName );
            ddlTemplates.DataTextField = "DisplayName";
            ddlTemplates.DataValueField = "ID";
            ddlTemplates.DataBind();
            ddlTemplates.Items.Insert(0, "");
        }

        private TemplatePicker CreateControl(TypeTemplate template)
        {
            TemplatePicker picker = (TemplatePicker)LoadControl("TemplatePicker.ascx");
            picker.RemoveTemplate += RemoveTemplate;
            picker.Template = template;
            return picker;
        }

        #endregion
    }
}